package com.dx.as.common;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;

import com.dx.as.common.log.LogUtil;

import java.lang.ref.SoftReference;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Created by jwaoo on 2017/7/1.
 */

public class FragmentUtils {
    private static Map<String,SoftReference<Fragment>> map = new HashMap<>();
    private static Fragment previousFragment;
    private static Fragment previousChildFragment;
    private static List<String> fragmentList = new ArrayList<>();
    public static void goToFragment(Activity activity , Class<? extends Fragment> fragmentClass, int container){
        FragmentManager fragmentManager = activity.getFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        String fragmentName = fragmentClass.getSimpleName();
        SoftReference<Fragment> soft = map.get(fragmentName);
        Fragment fragment;
        try{
            fragmentTransaction.hide(previousFragment);
            if (soft == null){
                fragment = fragmentClass.newInstance();
                fragmentTransaction.add(container, fragment);
                map.put(fragmentName, new SoftReference<>(fragment));
                fragmentList.add(fragment.getClass().getSimpleName());
            }else{
                fragment = soft.get();
                fragmentTransaction.show(fragment);
                String value =fragment.getClass().getSimpleName();
                if (fragmentList.contains(value)){
                    fragmentList.remove(value);
                    fragmentList.add(value);
                }
            }
            previousFragment = fragment;
            fragmentTransaction.commit();
        }catch(Exception ex){
            ex.printStackTrace();
        }
    }

    public static void goToChildFragment(Fragment activity , Class<? extends Fragment> fragmentClass, int container){
        FragmentManager fragmentManager = activity.getChildFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        String fragmentName = fragmentClass.getSimpleName();
        SoftReference<Fragment> soft = map.get(fragmentName);
        Fragment fragment;
        try{
            fragmentTransaction.hide(previousChildFragment);
            if (soft == null){
                fragment = fragmentClass.newInstance();
                fragmentTransaction.add(container, fragment);
                map.put(fragmentName, new SoftReference<>(fragment));
                fragmentList.add(fragment.getClass().getSimpleName());
            }else{
                fragment = soft.get();
                fragmentTransaction.show(fragment);
                String value =fragment.getClass().getSimpleName();
                if (fragmentList.contains(value)){
                    fragmentList.remove(value);
                    fragmentList.add(value);
                }
            }
            previousChildFragment = fragment;
            fragmentTransaction.commit();
        }catch(Exception ex){
            ex.printStackTrace();
        }
    }

    public static void goBackFragment(Activity activity , Class<? extends Fragment> fragmentClass, int container,Class<? extends Fragment> cls){
        goToFragment(activity , fragmentClass, container);
        clearFragmentByClass(cls);
    }

    public static void putDefaultFragment(Activity activity ,int container ,Fragment fragment){
        FragmentManager fragmentManager = activity.getFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        try{
            fragmentTransaction.add(container, fragment);
            fragmentTransaction.commit();
            previousFragment = fragment;
            map.put(fragment.getClass().getSimpleName(), new SoftReference<>(fragment));
            fragmentList.add(fragment.getClass().getSimpleName());
        }catch(Exception ex){
            ex.printStackTrace();
        }
    }

    public static void putDefaultFragment(Fragment activity ,int container ,Fragment fragment){
        FragmentManager fragmentManager = activity.getChildFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        try{
            fragmentTransaction.add(container, fragment);
            fragmentTransaction.commit();
            previousChildFragment = fragment;
            map.put(fragment.getClass().getSimpleName(), new SoftReference<>(fragment));
            fragmentList.add(fragment.getClass().getSimpleName());
        }catch(Exception ex){
            ex.printStackTrace();
        }
    }

    public static void clearFragment(){
        Set<String> set = map.keySet();
        Iterator<String> iterator = set.iterator();
        try {
            while (iterator.hasNext()){
                map.get(iterator.next()).get().onDestroy();
            }
        }catch (Exception ex){
            LogUtil.d("FragmentUtils",ex.toString());
        }
        map.clear();
        fragmentList.clear();
    }

    public static void clearFragmentByClass(Class<?> cls){
        String key = cls.getSimpleName();
        SoftReference<Fragment> softReference = map.get(cls.getSimpleName());
        if (softReference != null)
            softReference.clear();
        map.remove(key);
        fragmentList.remove(key);
        if (!map.isEmpty()){
            previousFragment = map.get(fragmentList.get(fragmentList.size()-1)).get();
            LogUtil.d("FragmentUtils",previousFragment.getClass().getSimpleName());
        }
    }

    public static Fragment getFragmentByClassName(Class<?> cls){
        return map.get(cls.getSimpleName()).get();
    }

    public static Fragment getCurrentFragment(){
        return previousFragment;
    }
}
